package com.ruoyi.framework.web.service;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.system.domain.Myuser;
import com.ruoyi.system.domain.Userinfo;
import com.ruoyi.system.domain.Userskill;
import com.ruoyi.system.domain.vo.*;
import com.ruoyi.system.mapper.MyuserMapper;
import com.ruoyi.system.mapper.UserinfoMapper;
import com.ruoyi.system.mapper.UserskillMapper;
import com.ruoyi.system.service.RedisService;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class LoginServiceImpl {
    @Autowired
    private MyuserMapper myuserMapper;
    @Autowired
    private UserskillMapper userskillMapper;
    @Autowired
    private UserinfoMapper userinfoMapper;
    @Autowired
    private TokenService tokenService;

    @Autowired
    private RedisService redisService;
    public Results login(LoginDTO loginDTO) {
        if (StringUtils.isEmpty(loginDTO.getLoginName())){return new Results(400,"账号不能为空","");}
        if (StringUtils.isEmpty(loginDTO.getPassword())){return new Results(400,"密码不能为空","");}
        //通过登录名查询用户
        Myuser myuser = myuserMapper.selectMyuserByName(loginDTO.getLoginName());
        if( myuser==null){return new Results(404,"该账户不存在","");}
        long time =  myuser.getNdate().getTime();
        Date nowdate = new Date();
        if((nowdate.getTime()-time)>2*60*60*1000){myuser.setStatus(0L);myuser.setNdate(nowdate);myuserMapper.updateMyuser(myuser);}
        if(myuser.getStatus()==1){return new Results(405,"该账户已经登陆了","");}
        //比较密码
        if (myuser!=null&&myuser.getPassword().equals(loginDTO.getPassword())){
            LoginVO loginVO=new LoginVO();
            loginVO.setId(Integer.valueOf(String.valueOf(myuser.getUserid())));
            //这里token直接用一个uuid
            //使用jwt的情况下，会生成一个jwt token,jwt token里会包含用户的信息
            LoginUser loginUser = new LoginUser();loginUser.setUserId(myuser.getUserid());SysUser sysUser = new SysUser();
            sysUser.setUserName(myuser.getUserName());sysUser.setPassword(myuser.getPassword());loginUser.setUser(sysUser);
            String token = tokenService.createToken(loginUser);
            loginVO.setToken(token);loginVO.setUsername(myuser.getUserName());myuser.setStatus(1L);
            Userinfo userinfo = userinfoMapper.selectUserinfoByUid(myuser.getUserid());
            myuserMapper.updateMyuser(myuser);
            List<Userskill> userskills = userskillMapper.selectUserskillList(myuser.getUserid(),userinfo.getSkill());
            List<SkillTable> skillTables = new ArrayList<SkillTable>();
            for(Userskill userskill : userskills){
                SkillTable skillTable = new SkillTable(Integer.parseInt(userskill.getSkillid().toString()),Integer.parseInt(userskill.getUserskill().toString()),"",userskill.getSkillname());
                skillTables.add(skillTable);}
            loginVO.setList(skillTables);
            loginVO.setImage(myuser.getImage());
            Integer loginTotalCount =(Integer) redisService.get("LoginTotalCount");
            if(loginTotalCount==null){
                redisService.set("LoginTotalCount",1);
            }else{
                redisService.incr("LoginTotalCount",1);
            }
            Integer loginCount =(Integer) redisService.get("LoginCount");
            if(loginCount==null){
                redisService.set("LoginCount",1);
            }else{
                redisService.incr("LoginCount",1);
            }
            return new Results(200,"",loginVO);}
        return new Results(401,"账号或者密码错误！","");
    }
}
